// config/config.default.js
'use strict';
const path = require('path');

// 🔑 必须的 keys 字段
exports.keys = 'xiaocai-item-2025-05-19-secret-key';

// 🛡️ 安全配置
exports.security = {
  csrf: {
    enable: false,
  },
  domainWhiteList: ['*.zcy.gov.cn'],
};

// 🔐 JWT 配置
exports.jwt = {
  secret: 'ZCY-ERP-SECRET-2023',
  enable: true,
  match: /^\/api/,
};

// 🧱 Redis 配置
const redisClientConfig = {
  host: '127.0.0.1',
  port: 6379,
  password: null,
};
exports.redis = {
  client: {
    ...redisClientConfig,
    db: 0,
  },
};

// 🗃️ Sequelize 数据库配置
exports.sequelize = {
  dialect: 'mysql',
  dialectOptions: {
    decimalNumbers: true,
  },
  timezone: '+08:00',
  define: {
    paranoid: true,
    underscored: true,
  },
  replication: {
    write: {
      host: 'rm-bp1z3054p.mysql.rds.aliyuncs.com',
      username: 'zcy_prod',
      password: process.env.MYSQL_PWD,
      port: 3306,
    },
    read: [
      {
        host: 'rm-bp1d.mysql.rds.aliyuncs.com',
        username: 'zcy_prod_ro',
        password: process.env.MYSQL_PWD_RO,
        port: 3306,
      },
    ],
  },
};

// 🐂 BullMQ 队列配置
exports.bullmq = {
  defaultQueueOptions: {
    connection: redisClientConfig,
    defaultJobOptions: {
      attempts: 3,
      backoff: {
        type: 'exponential',
        delay: 5000
      },
      removeOnComplete: true,
      removeOnFail: false
    }
  },
  queues: [
    { name: 'order-sync', concurrency: 2 },
    { name: 'finance-reconciliation', concurrency: 1 },
    { name: 'supplier-audit', concurrency: 3 }
  ]
};

// ⚠️ 熔断器配置
exports.circuitBreaker = {
  redis: {
    failureThreshold: 3,
    successThreshold: 2,
    timeout: 30000,
  },
};

// 🧹 中间件配置
exports.middleware = ['dataScope', 'blacklist', 'redisHealth'];

// 🌐 政采云专用配置
exports.zcyConfig = {
  apiEndpoint: 'https://open.zcy.gov.cn/gateway',
  appKey: process.env.ZCY_APP_KEY || '',
  appSecret: process.env.ZCY_APP_SECRET || '',
  provinceCodes: (() => {
    try {
      return JSON.parse(process.env.PROVINCE_CODES || '[]');
    } catch (error) {
      console.error('解析PROVINCE_CODES失败，使用默认值', error);
      return ['330000', '110000', '310000'];
    }
  })(),
};

// 📞 阿里云短信服务配置
exports.aliyunSms = {
  accessKeyId: process.env.ALIYUN_SMS_ACCESS_KEY,
  secret: process.env.ALIYUN_SMS_SECRET,
  signName: process.env.ALIYUN_SMS_SIGN_NAME,
  templates: {
    priceAlert: process.env.ALIYUN_SMS_TEMPLATE_PRICE_ALERT,
    blacklistNotice: process.env.ALIYUN_SMS_TEMPLATE_BLACKLIST,
  },
};

// 📄 日志监控配置
exports.logger = {
  dir: path.join(__dirname, '../logs'),
  consoleLevel: 'INFO',
  files: [
    {
      type: 'file',
      level: 'ERROR',
      filename: path.join(__dirname, '../logs/common-error.log'),
    },
  ],
};

// 🕒 定时任务开关
exports.schedule = {
  enable: true,
};